package com.yjhb.android.sbz.fragment;

import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.constraintlayout.widget.ConstraintLayout;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.yjhb.android.sbz.R;
import com.yjhb.android.sbz.adapter.GameAdapter;
import com.yjhb.android.sbz.entity.Game;
import com.yjhb.android.sbz.mvp.game.GamePresenter;
import com.yjhb.android.sbz.mvp.game.GameView;
import com.yjhb.android.sbz.tool.DYTool;
import com.yjhb.android.sbz.tool.JXWTool;
import com.yjhb.android.sbz.tool.XQTool;
import com.yjhb.android.sbz.tool.XWTool;
import com.jojo.android.zxlib.base.BaseFragment;
import com.jojo.android.zxlib.base.BaseRecyclerAdapter;
import com.jojo.android.zxlib.tool.ZxImageTool;
import com.jojo.android.zxlib.view.EmptyRecyclerView;
import com.jojo.android.zxlib.view.EmptyView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 创建者 zx
 * 创建时间 2021/1/15 9:04
 */
public class GameFragment extends BaseFragment<GamePresenter> implements GameView {
    private final String TAG = "GameFragment";
    @BindView(R.id.top_bg)
    ConstraintLayout topBg;
    @BindView(R.id.emptyView)
    EmptyView emptyView;
    @BindView(R.id.title_tv)
    TextView titleTv;
    @BindView(R.id.top_icon)
    ImageView topIcon;
    @BindView(R.id.top_name)
    TextView topName;
    @BindView(R.id.top_depict)
    TextView topDepict;
    @BindView(R.id.top_start_tv)
    TextView topStartTv;
    @BindView(R.id.dy)
    LinearLayout dy;
    @BindView(R.id.xq)
    LinearLayout xq;
    @BindView(R.id.xw)
    LinearLayout xw;
    @BindView(R.id.jxw)
    LinearLayout jxw;
    @BindView(R.id.jxw_iv)
    ImageView jxw_iv;
    @BindView(R.id.recyclerView)
    EmptyRecyclerView recyclerView;
    @BindView(R.id.swipe_recyclerview)
    SwipeRefreshLayout swipe_recyclerview;

    private GameAdapter gameAdapter;
    private Game topGame;
    private List<Game> listGame = new ArrayList<>();

    @Override
    protected GamePresenter initPresenter() {
        return new GamePresenter(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_game;
    }

    @Override
    protected void initUI() {
        DYTool.init(getContext());
        XQTool.init(getContext());
        XWTool.init(getContext());
        JXWTool.init(getContext());
//        Glide.with(getContext()).load(R.mipmap.jxw2_icon).into(jxw_iv);
        swipe_recyclerview.setColorSchemeColors(getResources().getColor(R.color.colorPrimary),
                getResources().getColor(R.color.colorPrimaryDark),
                getResources().getColor(R.color.colorPrimaryOtherDark));
        recyclerView.setAdapter(gameAdapter = new GameAdapter(getContext()));
        gameAdapter.setData(listGame);
    }

    @Override
    protected void initEvent() {
        swipe_recyclerview.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                presenter.getGames();
            }
        });
        emptyView.setOnReloadClickListener(new EmptyView.OnReloadClickListener() {
            @Override
            public void onReloadClick(EmptyView emptyView) {
                emptyView.showLoading();
                presenter.getGames();
            }
        });
        gameAdapter.setOnItemClick(new BaseRecyclerAdapter.ItemClickListener<Game>() {
            @Override
            public void onItemClick(int index, Game game) {

            }

            @Override
            public void onViewClick(View view, String msg, int index, Game game) {
                startPage(game);
            }
        });
    }

    @Override
    protected void initData() {
        presenter.getGames();
        emptyView.setVisibility(View.VISIBLE);
        emptyView.showLoading();
    }

    private void refresh() {
        if (topGame != null) {
            topName.setText(topGame.getGameName());
            initTop(topGame.getIcon());
            topDepict.setText(topGame.getDepict());
        }
    }

    private void startPage(Game game) {
        if (game == null) return;
        switch (game.getCompany()) {
            //多游
            case 1:
                DYTool.getInstance().startDetailPage(game.getGameId());
                break;
            //嘻趣
            case 2:
                XQTool.getInstance().startDetailPage(game.getGameId());
                break;
            //闲玩
            case 3:
                XWTool.getInstance().startDetailPage(game.getGameId());
                break;
            //聚享玩
            case 4:
                JXWTool.getInstance().startDetailPage(game.getGameId());
                break;
        }
    }


    private void initTop(String icon) {
        //初始化顶部背景
        Glide.with(getContext())
                .asBitmap()
//                .load("https://imgs.ali213.net/oday/uploadfile/2020/07/27/202007275295885.jpg")
                .load(icon)
                .into(new CustomTarget<Bitmap>() {
                    @Override
                    public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                        Bitmap bitmap = ZxImageTool.fastBlur(getContext(), resource, 0.3f, 15, false);
                        topBg.setBackground(new BitmapDrawable(getResources(), bitmap));
                    }

                    @Override
                    public void onLoadCleared(@Nullable Drawable placeholder) {
                        topBg.setBackground(new ColorDrawable(0));
                    }
                });
        //设置顶部图标
        Glide.with(getContext()).load(icon).into(topIcon);
    }

    @OnClick(R.id.top_start_tv)
    void topStart() {
        if (topGame != null) {
            startPage(topGame);
        }
    }

    @OnClick(R.id.dy)
    void dy() {
        DYTool.getInstance().startList();
    }

    @OnClick(R.id.xq)
    void xq() {
        XQTool.getInstance().startList();
    }

    @OnClick(R.id.xw)
    void xw() {
        XWTool.getInstance().startList();
    }

    @OnClick(R.id.jxw)
    void jxw() {
        JXWTool.getInstance().startList();
    }

    @Override
    public void onLoad(List<Game> games) {
        if (games != null) {
            if (games.size() > 0) {
                topGame = games.remove(0);
            }
            this.listGame.clear();
            this.listGame.addAll(games);
        }
        refresh();
        swipe_recyclerview.setRefreshing(false);
        emptyView.setVisibility(View.GONE);
        gameAdapter.notifyDataSetChanged();
    }

    @Override
    public void onFailure(int code, String msg) {
        emptyView.showFail(msg, true);
        emptyView.setVisibility(View.VISIBLE);
        swipe_recyclerview.setRefreshing(false);
    }
}
